package cn.cqie.attendance.manager.service.impl;

import cn.cqie.attendance.common.util.Page;
import cn.cqie.attendance.common.util.PageBean;
import cn.cqie.attendance.manager.controller.AdminCourseController;
import cn.cqie.attendance.manager.dao.AdminCourseDao;
import cn.cqie.attendance.manager.dao.AdminGroupDao;
import cn.cqie.attendance.manager.pojo.*;
import cn.cqie.attendance.manager.service.AdminCourseService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 697
 * @date 2021/7/2 14:35
 */
@Service
public class AdminCourseServiceImpl implements AdminCourseService {

    @Autowired
    private AdminCourseDao adminCourseDao;

    @Autowired
    private AdminGroupDao adminGroupDao;


    /**
     * 查询课程信息 可组合模糊查询
     *
     * @param adminCourseSerch
     * @param pageSize         每页的数量
     * @param pageNum      当前页
     * @return
     */
    @Override
    public PageBean<AdminCourse> queryAllCourseInfo(AdminCourseSerch adminCourseSerch, Integer pageSize, Integer pageNum) {

        if (pageNum == null || pageSize == null) {
            pageNum = 1;
            pageSize = 10;
        }
        PageHelper.startPage(pageNum,pageSize);
        List<AdminCourse> list = adminCourseDao.queryAllCourseInfo(adminCourseSerch);
        int total = list.size();
        if (null == list || total == 0) {
            return new PageBean<>(201,"error",list);
        }
        return new PageBean<>(200,"success",list);
    }


    /**
     * 根据ID查询课程信息
     *
     * @param courseId id
     * @return
     */
    @Override
    public Page<AdminCourse> queryCourseInfoById(Integer courseId) {
        List<AdminCourse> list = adminCourseDao.queryCourseInfoById(courseId);
        int total = list.size();
        if (null == list || total == 0) {
            return Page.INFOERROR;
        }
        return new Page<>(200, "Success", 0, 0, total, list);
    }


    /**
     * 查询所有老师 用于选择实训老师
     *
     * @return
     */
    @Override
    public Page<AdminTeacher> queryChooseTrainerTeacher() {
        List<AdminTeacher> list = adminCourseDao.queryChooseTrainerTeacher();
        int total = list.size();
        if (null == list || total == 0) {
            return Page.INFOERROR;
        }
        return new Page<>(200, "Success", 0, 0, total, list);
    }


    /**
     * 新增课程信息 不包括实训老师
     *
     * @param adminCourse
     * @return
     */
    @Override
    public int addCourseInfoNoTrainTeacher(AdminCourse adminCourse) {
        int i = adminCourseDao.addCourseInfoNoTrainTeacher(adminCourse);
        if (i > 0) {
            return 1;
        } else {
            return 0;
        }
    }


    /**
     * 新增实训老师
     *
     * @param adminTrainTeacherCourse
     * @return
     */
    @Override
    public int addTrainTeacher(AdminTrainTeacherCourse adminTrainTeacherCourse) {
        int i = adminCourseDao.addTrainTeacher(adminTrainTeacherCourse);
        if (i > 0) {
            return 1;
        } else {
            return 0;
        }
    }

    /**
     * 判断是否存在课程名重复-》课程时间重复的情况
     * @param adminCourse
     * @return
     */
//    @Override
//    public int queryCourseTimeCount(AdminCourse adminCourse) {
//
//        return adminCourseDao.queryCourseTimeCount(adminCourse);
//    }


    /**
     * 新增课程信息
     *
     * @param adminCourse      课程信息
     * @param listTrainTeacher 实训老师
     * @return
     */
    @Override
    public Page addCourseInfo(AdminCourse adminCourse, List<AdminTrainTeacherCourse> listTrainTeacher) {

        //创建实训老师课程表 对象
        AdminTrainTeacherCourse adminTrainTeacherCourse = new AdminTrainTeacherCourse();
        int i2 = 0;
        //获取当前时间当作课程的创建时间
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
        adminCourse.setCreateTime(df.format(new Date()));
        //新增课程信息 先不新增实训老师
        //判断课程是否重复
        int i11 = adminCourseDao.queryCourseTimeCount(adminCourse);
        int i1 = adminCourseDao.addCourseInfoNoTrainTeacher(adminCourse);
//        System.out.println("-------------------");
//        System.out.println("测试返回的主键："+adminCourse.getId());
//        System.out.println("-------------------");
        //从这里开始判断实训老师
        for (int i = 0; i < listTrainTeacher.size(); i++) {
            int trainTeacherId = listTrainTeacher.get(i).getTeacherId();//实训老师id
            String trainTeacherName = listTrainTeacher.get(i).getTrainTeacherName();//实训老师名字
            //赋值进去 adminTrainTeacherCourse
            adminTrainTeacherCourse.setCourseId(adminCourse.getId());//课程id
            adminTrainTeacherCourse.setTrainTeacherName(trainTeacherName);//实训老师名字
            adminTrainTeacherCourse.setTeacherId(trainTeacherId);//实训老师id
            //新增实训老师
            i2 = adminCourseDao.addTrainTeacher(adminTrainTeacherCourse);
        }
        //判断是否成功
        if ((i1 > 0 || i2 > 0) && i11<1) {
            return Page.SUCCESS;
        } else {
            return Page.FAIL;
        }

    }


    /**
     * 根据id修改课程信息 不包括实训老师
     *
     * @param adminCourse
     * @return
     */
    @Override
    public int updateCourseByIdNoTrainTeacher(AdminCourse adminCourse) {
        int i = adminCourseDao.updateCourseByIdNoTrainTeacher(adminCourse);
        return i;
    }


    /**
     * 根据课程id修改实训老师
     *
     * @param adminTrainTeacherCourse
     * @return
     */
    @Override
    public int updateTrainTeacher(AdminTrainTeacherCourse adminTrainTeacherCourse) {
        int i = adminCourseDao.updateTrainTeacher(adminTrainTeacherCourse);
        return i;
    }


    /**
     * 根据课程id修改课程信息
     *
     * @param adminCourse      课程
     * @param listTrainTeacher 课程实训老师
     * @return
     */
    @Override
    public Page updateCourseById(AdminCourse adminCourse, List<AdminTrainTeacherCourse> listTrainTeacher) {
        //创建实训老师课程表 对象
        AdminTrainTeacherCourse adminTrainTeacherCourse = new AdminTrainTeacherCourse();
        int i2 = 0;

        //修改课程信息 先不修改实训老师
        int i1 = adminCourseDao.updateCourseByIdNoTrainTeacher(adminCourse);

        //查看是否重复
        int i11 = adminCourseDao.queryCourseTimeCount(adminCourse);

        int courseId = adminCourse.getId();
        int i3 = adminCourseDao.deleteTrainerTeacherByCourseId(courseId);
        if (i3 > 0) {
            //从这里开始判断实训老师
            for (int i = 0; i < listTrainTeacher.size(); i++) {
                int trainTeacherId = listTrainTeacher.get(i).getTeacherId();//实训老师id
                String trainTeacherName = listTrainTeacher.get(i).getTrainTeacherName();//实训老师名字
                //赋值进去 adminTrainTeacherCourse
                adminTrainTeacherCourse.setCourseId(courseId);//课程id
                adminTrainTeacherCourse.setTrainTeacherName(trainTeacherName);//实训老师名字
                adminTrainTeacherCourse.setTeacherId(trainTeacherId);//实训老师id
                //修改实训老师
                i2 = adminCourseDao.addTrainTeacher(adminTrainTeacherCourse);
            }
        }
        //判断是否成功
        if ((i1 > 0 || i2 > 0) && i11<1) {
            return Page.SUCCESS;
        } else {
            return Page.FAIL;
        }

    }


    /**
     * 根据课程id删除实训老师
     *
     * @param courseId 课程id
     * @return
     */
    @Override
    public int deleteTrainerTeacherByCourseId(Integer courseId) {
        int i = adminCourseDao.deleteTrainerTeacherByCourseId(courseId);
        return i;
    }


    /**
     * 课程选择学生
     *
     * @param listGroupId
     * @param courseId
     * @return
     */
    @Override
    public Page addStudentToCourse(List listGroupId, Integer courseId, List listStudentId) {
        //当没有选择学生  只选择分组的时候我们就用分组的信息来新增
        if (null == listStudentId || listStudentId.size() == 0) {
            AdminStudent adminStudent = new AdminStudent();
            AdminCourseStudentMsg adminCourseStudentMsg = new AdminCourseStudentMsg();
            int i1 = 0;
            //先遍历listGroupId查到每个分组下面的学生
            for (int i = 0; i < listGroupId.size(); i++) {
                List<AdminStudent> listStudent = adminGroupDao.queryStudentByGroupIdCombination(adminStudent, (int) listGroupId.get(i));
                int i2 = 0;
                //在把学生加入到数据库课程里面
                for (int j = 0; j < listStudent.size(); j++) {
                    adminCourseStudentMsg.setStuId(listStudent.get(j).getId());//学生id
                    adminCourseStudentMsg.setCourseId(courseId);//课程id
                    adminCourseStudentMsg.setRole(listStudent.get(j).getDirection());//学生方向
                    //先判断课程中是否存在重复学生 不存在就添加进去
                    i2 = adminCourseDao.queryCountStudentInCourse(adminCourseStudentMsg);
                    if (i2 < 1) {
                        i1 = adminCourseDao.addStudentToCourse(adminCourseStudentMsg);
                        i1++;
                    }
                }
            }
            if (i1 > 0) {
                return Page.SUCCESS;
            } else {
                return Page.FAIL;
            }
        }
        //当既选择分组又选择学生的时候我们就用学生信息新增
        else {
            AdminStudent adminStudent = new AdminStudent();
            AdminCourseStudentMsg adminCourseStudentMsg = new AdminCourseStudentMsg();
            int i2 = 0;
            int i1 = 0;
            for (int i = 0; i < listStudentId.size(); i++) {
                int studentId = (int) listStudentId.get(i);
                //根据学生id查询角色
                List<AdminStudent> list = adminCourseDao.queryStudentByStudentId(studentId);
                int role = list.get(0).getDirection();
                adminCourseStudentMsg.setStuId(studentId);//学生id
                adminCourseStudentMsg.setRole(role);//角色
                adminCourseStudentMsg.setCourseId(courseId);
                //先判断课程中是否存在重复学生 不存在就添加进去
                i2 = adminCourseDao.queryCountStudentInCourse(adminCourseStudentMsg);
                if (i2 < 1) {
                    i1 = adminCourseDao.addStudentToCourse(adminCourseStudentMsg);
                    i1++;
                }else{
                    return Page.INFOERROR;
                }
            }
            if (i1 > 0) {
                return Page.SUCCESS;
            } else {
                return Page.FAIL;
            }
        }
    }


    /**
     * 查询课程中某个学生数量 用来判断学生是否重复
     *
     * @param adminCourseStudentMsg
     * @return
     */
    @Override
    public int queryCountStudentInCourse(AdminCourseStudentMsg adminCourseStudentMsg) {
        int i = adminCourseDao.queryCountStudentInCourse(adminCourseStudentMsg);
        return i;
    }


    /**
     * 查询选择所有分组下面的学生
     *
     * @param listGroupId
     * @return
     */
    @Override
    public Page<List<AdminStudent>> queryStudentByAllGroupId(List listGroupId) {

        List<List<AdminStudent>> listTotal = new ArrayList<>();
        List<AdminStudent> listStudent = new ArrayList<>();
        //遍历listGroupId查询学生
        for (Object o : listGroupId) {
            listStudent = adminCourseDao.queryStudentByGroupId((Integer) o);
            listTotal.add(listStudent);
        }
        int total = listTotal.size();
        if (null == listStudent || total == 0) {
            return Page.INFOERROR;
        }
        return new Page<>(200, "Success", 0, 0, total, listTotal);


    }


    /**
     * 根据学生id查询详情
     *
     * @param studentId
     * @return
     */
    @Override
    public Page<AdminStudent> queryStudentByStudentId(Integer studentId) {
        List<AdminStudent> list = adminCourseDao.queryStudentByStudentId(studentId);
        int total = list.size();
        if (null == list || total == 0) {
            return Page.INFOERROR;
        }
        return new Page<>(200, "Success", 0, 0, total, list);

    }


    /**
     * 根据分组id查询学生
     *
     * @param groupId 分组id
     * @return
     */
    @Override
    public Page<AdminStudent> queryStudentByGroupId(Integer groupId) {
        List<AdminStudent> list = adminCourseDao.queryStudentByGroupId(groupId);
        int total = list.size();
        if (null == list || total == 0) {
            return Page.INFOERROR;
        }
        return new Page<>(200, "Success", 0, 0, total, list);

    }


    /**
     * 根据课程ID删除课程信息
     *
     * @param courseId 课程ID
     * @return
     */
    @Override
    public Page deleteCourseById(Integer courseId) {
        int i = adminCourseDao.deleteCourseById(courseId);
        if (i > 0) {
            return Page.SUCCESS;
        } else {
            return Page.INFOERROR;
        }
    }


    /**
     * 复制课程信息
     * @param adminCourse      课程
     * @param listTrainTeacher 实训老师
     * @return
     */
    public Page CopyCourseInfoById(AdminCourse adminCourse, List<AdminTrainTeacherCourse> listTrainTeacher,Integer courseId) {

        //创建实训老师课程表 对象
        AdminTrainTeacherCourse adminTrainTeacherCourse = new AdminTrainTeacherCourse();
        int i2 = 0;
        //获取当前时间当作课程的创建时间
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
        adminCourse.setCreateTime(df.format(new Date()));
        //复制课程信息 先不复制实训老师
        int i1 = adminCourseDao.addCourseInfoNoTrainTeacher(adminCourse);
        //从这里开始判断实训老师
        for (int i = 0; i < listTrainTeacher.size(); i++) {
            int trainTeacherId = listTrainTeacher.get(i).getTeacherId();//实训老师id
            String trainTeacherName = listTrainTeacher.get(i).getTrainTeacherName();//实训老师名字
            //赋值进去 adminTrainTeacherCourse
            adminTrainTeacherCourse.setCourseId(adminCourse.getId());//新课程id
            adminTrainTeacherCourse.setTrainTeacherName(trainTeacherName);//实训老师名字
            adminTrainTeacherCourse.setTeacherId(trainTeacherId);//实训老师id
            //在新课程里面复制实训老师
            i2 = adminCourseDao.addTrainTeacher(adminTrainTeacherCourse);
        }

        //复制学生进课程
        List<AdminCourseStudentMsg> list = adminCourseDao.queryStudentByCourseId(courseId);//根据原班级id查询学生
        int i3 = 0;
        int i4 = 0;
        AdminCourseStudentMsg adminCourseStudentMsg = new AdminCourseStudentMsg();
        for (int i = 0;i<list.size();i++){
            int studentId = list.get(i).getStuId();
            int role = list.get(i).getRole();
            adminCourseStudentMsg.setCourseId(adminCourse.getId());//新班级id
            adminCourseStudentMsg.setStuId(studentId);//学生id
            adminCourseStudentMsg.setRole(role);//角色
            //先判断课程中是否存在重复学生 不存在就添加进去
            i3 = adminCourseDao.queryCountStudentInCourse(adminCourseStudentMsg);
            if (i3 < 1) {
                i4 = adminCourseDao.addStudentToCourse(adminCourseStudentMsg);
                i4++;
            }else{
                return Page.INFOERROR;
            }
        }

        //判断是否成功
        if (i1 > 0 || i2 > 0 || i4 > 0) {
            return Page.SUCCESS;
        } else {
            return Page.FAIL;
        }

    }


    /**
     * 根据班级id查询该班级下的学生 用于复制
     * @param courseId
     * @return
     */
    @Override
    public Page<AdminCourseStudentMsg> queryStudentByCourseId(Integer courseId) {
        List<AdminCourseStudentMsg> list = adminCourseDao.queryStudentByCourseId(courseId);
        int total = list.size();
        if (null == list || total == 0) {
            return Page.INFOERROR;
        }
        return new Page<>(200, "Success", 0, 0, total, list);
    }


    /**
     * 复制学生进课程
     * @param courseId 课程
     * @return
     */
    @Override
    public Page<AdminCourseStudentMsg> copyStudentToCourse(Integer courseId) {
        List<AdminCourseStudentMsg> list = adminCourseDao.queryStudentByCourseId(courseId);
        AdminCourseStudentMsg adminCourseStudentMsg = new AdminCourseStudentMsg();
        for (int i = 0;i<list.size();i++){
            int studentId = list.get(i).getStuId();
            int role = list.get(i).getRole();
            adminCourseStudentMsg.setCourseId(courseId);//班级id
            adminCourseStudentMsg.setStuId(studentId);//学生id
            adminCourseStudentMsg.setRole(role);//角色
        }
        return Page.SUCCESS;
    }
}
